
use $datapath/clean/main_data.dta, clear 

	
***********************************************		
** Create logit demand variables 
	
reshape long Q N iv, i(popu_lsa year lno ) j(type) s
	
	gen double M = 50*popu_lsa
	gen double s = Q/(N*M) 
	
	keep if s>0 & s<1 
	
	gen de = type=="e"
	egen double QQ = sum(Q), by(lno year)
	egen double NN = sum(N), by(lno year)
		
	gen double ls = log(s) - log((M - QQ)/M)
 
	gen double lstop = log(Q/QQ)
	gen double lstop_inst = log(N/NN)

	gen lsbottom = log(1/N)
	
	
	label var ls "$/ln(s_j) - /ln(S_0)$"
	label var lstop_inst "$/ln(N_f/N)$"
	label var lsbottom "$/sigma_{1}$"
	label var lstop "$/sigma_{2}$"
	label var de "$/gamma^{/text{ebook}}$"	
***********************************************		
	
	
***********************************************		
** Table 4: nested logit demand estimates and table

eststo clear 
	
** 2-level NL, no instrument
	eststo: reghdfe ls de lstop lsbottom     i.year, absorb(lno) vce(robust)

** 1st stage:
	eststo: reghdfe lstop de lstop_inst  i.year if ls!=., absorb(lno)  vce(robust)
	
** 2-level NL, with instrument
	eststo: ivreghdfe ls  (lstop =   lstop_inst ) lsbottom    i.year de, absorb(lno)  robust
		estimates save output/logit_demand, replace

	
	local pattern prefix(/multicolumn{@span}{c}{) suffix(}) span erepeat(/cmidrule(lr){@span})

	esttab,  noomitted mtitles("$/ln(s_j) - /ln(S_0)$" "$/ln(s_{f|/text{inside}})$" "$/ln(s_j) - /ln(S_0)$" ) drop(*.year _cons) se replace  label nonotes star(* 0.10 ** 0.05 *** 0.01) scalars("r2_a $/overline{R^2}$") nocons

	esttab using "$tablepath/table5_nested_logit_demand.tex",  noomitted mtitles("OLS" "1st stage" "IV" ) drop(*.year _cons) se replace  label nonotes star(* 0.10 ** 0.05 *** 0.01) scalars("r2_a $/overline{R^2}$") nocons booktabs

***********************************************		
